//========encoding:utf-8=========
/*
 * @describe:  三目运算符
 * @author:mhao
 * @copyright:chinasoft.cn
 * @date:2020年4月1日   上午11:27:41
 */
package edu.chinasoft.handler;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import edu.chinasoft.domain.Application;
import edu.chinasoft.domain.Approval;
import edu.chinasoft.domain.Item;
import edu.chinasoft.domain.User;
import edu.chinasoft.service.ItemService;

@Controller("itemHandler")
@RequestMapping("/itemHandler")
public class ItemHandler {
	 private static Logger logger = LoggerFactory.getLogger(ItemHandler.class);
	 @Autowired
	 @Qualifier("itemService")
	 private ItemService itemService;
	//添加请假单明细
	    @RequestMapping(value = "/addApplicationItem.do",method = {RequestMethod.POST,RequestMethod.GET})
	    public String addApplicationItem(Integer applicationId,Integer approvalId, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	        logger.info(" ItemHandler >>> addApplicationItem start.......");
	        logger.info("当前申请单的id为："+applicationId+"，审批者的id为："+approvalId);
	        Application application = new Application();
	        application.setApplicationId(applicationId);
	        User user = new User();
	        user.setUserId(applicationId);
	        Approval approval = new Approval();
	        approval.setApprovalUser(user);
	        Item item = new Item();
	        item.setApplication(application);
	        item.setApproval(approval);
	        //获取当前时间
	        Date date = new Date();
	        //调用服务层方法
	        boolean result = itemService.insertItem(item);
	        String resultUrl="";
	        if(result) {
	        	resultUrl="redirect:/applicationHandler/showNoSubmitApplication.do";
	        }else {
	        	resultUrl="/additemerror";
	        }
	        logger.info(" ItemHandler >>> addApplicationItem end.......");
			return resultUrl;
	    }	
	    //查询请假单明细
	    @RequestMapping(value="/showItem.do",method= {RequestMethod.POST,RequestMethod.GET})
	    public ModelAndView showItem(Integer applicationId,ModelAndView modelAndView,HttpSession session,HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
	    	 logger.info(" ItemHandler >>> showItem start.......");
	    	 logger.info("当前申请单的id为："+applicationId);
	    	 Application application = new Application();
	    	 application.setApplicationId(applicationId);
	    	 //修改日期格式
	    	 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	    	 Item writeItem =itemService.selectWriteTime(application);
	    	//填写时间
	    	 Date writeDate = writeItem.getOperationTime();
	    	 String writeDateString = simpleDateFormat.format(writeDate);
	    	 Item submitItem = itemService.selectSubmitTime(application);
	    	 String submitDateString=null;
	    	//提交时间
	    	 if(submitItem!=null) {
	    		 Date submitDate = submitItem.getOperationTime();
	    		 submitDateString = simpleDateFormat.format(submitDate);
	    	 }
	    	 logger.info(writeDateString+":"+submitDateString);
	    	 String[] timeArray = {writeDateString,submitDateString};
	    	 logger.info("当前timeArray为："+timeArray[0]+":"+timeArray[1]);
	    	 session.setAttribute("timeItem", timeArray);
	    	 List<Approval> approvalList = itemService.selectApprovalByItem(application);
	    	 //修改审核时间格式
	    	 Map<Integer, String> approvalMap = new HashMap<Integer,String>();
	    	 for(Approval approval:approvalList) {
	    		 Date approvalTime = approval.getApprovalTime();
	    		 logger.info("操作时间date为："+approvalTime);
	    		 String approvalTimeString = simpleDateFormat.format(approvalTime);
	    		 logger.info("操作时间string为："+approvalTimeString);
	    		 approvalMap.put(approval.getApprovalId(), approvalTimeString);
	    	 }
	    	 logger.info("字典大小为："+approvalMap);
	    	 Set<Integer> keySet = approvalMap.keySet();
	    	 for(Integer key:keySet) {
	    		 logger.info(key+":"+approvalMap.get(key));
	    	 }
	    	 session.setAttribute("approvalMap", approvalMap);
	    	 session.setAttribute("approvalListItem", approvalList);
	    	 modelAndView.setViewName("/showItem");
	    	 logger.info(" ItemHandler >>> showItem end.......");
			return modelAndView;
	    	
	    }
	    @InitBinder
	    protected void initBinder(WebDataBinder binder) {
	        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	        dateFormat.setLenient(false);
	        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	    }
}
